************************************************************************************************************************;
********************************************* Baseler program code Stata ***********************************************;
************************************************************************************************************************;
                                                                                                                    
clear all

*** Set version
version 15

*** Control monitor output
set more off
set logtype text
set linesize 255

*** Set date (for unique file naming)
local date 071724

*** Set path of ado
sysdir set PERSONAL "<Path is added at the Research Data Centre>"
mata mata mlib index

*** Macros for paths
global datapath "<Path is added at the Research Data Centre>\"
global outputpath "<Path is added at the Research Data Centre>\"

*** Macros for names of datasets and output
global dataname_census2011 zensus2011_prod1_sf.dta
global outputname Baseler_output_census_`date'

********************************************************************************************************************
*** I. Data processing
********************************************************************************************************************

*** a. Open dataset and variable selection
*** Direct selection of necessary variables (memory space- and runtime-efficient).

use ANSCHRIFT_ID WOHN_STAT_STP GESCHLECHT GEBOREN_JAHR STAATSANGE_LAND FAMSTAND_AUSF ZUZUG ZUZUG_JAHR ZUZUG_STAAT SCHULABS_AUSF AUSBILDUNGSABS_HOECHSTER HRF_ZUSATZ ZAEHLER BERUF_ISCO_HAUPTGR ERWERBSTAT_KURZ GLAUBENSRICHTUNG RELIGION_AUSF ///
using "$datapath\$dataname_census2011", clear

destring ANSCHRIFT_ID WOHN_STAT_STP GESCHLECHT GEBOREN_JAHR STAATSANGE_LAND FAMSTAND_AUSF ZUZUG ZUZUG_JAHR ZUZUG_STAAT SCHULABS_AUSF AUSBILDUNGSABS_HOECHSTER HRF_ZUSATZ ZAEHLER BERUF_ISCO_HAUPTGR ERWERBSTAT_KURZ GLAUBENSRICHTUNG RELIGION_AUSF, replace

keep if ZAEHLER==1
drop ZAEHLER

*** b. Generate new variables

*survey design variables
gen psuid = ANSCHRIFT_ID //primary sampling unit (address)
gen weight = HRF_ZUSATZ //extrapolation factor
gen f = 0.096 //finite population correction factor

* origin variables
gen Sov = inlist(ZUZUG_STAAT,127,139,142,146,160,166,169,422,425,430,444,450,470,477,741,471)
gen Sat = inlist(ZUZUG_STAAT,125,152,154,155,164,165)
gen EB = max(Sov,Sat)
gen west = inlist(ZUZUG_STAAT,123,124,126,128,129,134,135,136,137,141,143,145,147,148,149,153,156,157,158,161,167,168) //West, North, and South Europe (excl. Communist sphere)
gen west2 = inlist(ZUZUG_STAAT,123,124,126,129,135,137,143,145,147,148,156,158,167,168) // excluding countries that joined EU/EEA between 1974 and 2003: Greece, Portugal, Spain, Austria, Finland, Sweden, Iceland, Liechtenstein, Norway
gen Imm = max(west,EB)
gen Imm2 = max(west2,EB)
gen native = ZUZUG == 0

*combine source country vars
gen birth_country = ZUZUG_STAAT
replace birth_country = 0 if native == 1

*other demographic variables
gen female = (GESCHLECHT==2)
gen age = 2011 - GEBOREN_JAHR
gen married = FAMSTAND_AUSF == 2
gen jewish = RELIGION_AUSF == 5

* immigration status variables
gen immigration_year = ZUZUG_JAHR
gen immigration_age = age - (2011 - immigration_year) // age at immigration
	replace immigration_age = 0 if immigration_age < 0
	replace immigration_age = 900 if native == 1

gen years_in_germany = 2011 - immigration_year
	replace years_in_germany = 900 if native == 1

*Assign immigrants to year-of-arrival groups
gen post_87 = immigration_year>=1987 & immigration_year != .
gen post_93 = immigration_year>=1993 & immigration_year != .
gen post_89 = immigration_year>=1989 & immigration_year != . //collapse

*** Interaction Terms
foreach stub in EB Sov Sat Imm Imm2 {
	
* Origin * Arrival year
gen `stub'_post_87 = post_87*`stub'
gen `stub'_post_93 = post_93*`stub'
gen `stub'_post_89 = post_89*`stub'
}

*Individual year coefficeints from 1986 to 1999
foreach stub in EB Imm {
forvalues j = 86/98{
gen `stub'_`j' = `stub' * (immigration_year == 19`j')
}
}


*** Education recode to ISCED-1997
recode SCHULABS_AUSF 1 11 . = 1 2 = 4 3 31 32 = 5 4 5 = 6 12 = 3, generate (as)
recode AUSBILDUNGSABS_HOECHSTER . = 1 1 2 = 4 3 4 5 = 5 6 7 8 9 = 6 10 11 = 7 12 = 8 , generate (ba)

generate isced97 = 0
replace isced97 = 1 if (as == 1 & ba == 1)
replace isced97 = 3 if ((as == 0 | as == 1) & (ba == 3 | ba == 4)) | ((as == 3 | as == 4) & (ba == 0 | ba == 1 | ba == 3 | ba == 4))
replace isced97 = 4 if (as == 5 & (ba == 1 | ba == 3 | ba == 4))
replace isced97 = 5 if ((as == 0 | as == 1 | as == 3 | as == 4 | as == 5) & ba == 5)
replace isced97 = 6 if (as == 6 & (ba == 1 | ba == 3 | ba == 4))
replace isced97 = 7 if (as == 6 & ba == 5)
replace isced97 = 8 if (ba == 6)
replace isced97 = 9 if (ba == 7)
replace isced97 = 10 if (ba == 8)

drop as ba

gen edu_years = 0
	replace edu_years = 4 if isced97 == 1 //level 1
	replace edu_years = 10 if isced97 == 3 //level 2B
	replace edu_years = 11 if isced97 == 4 //level 2A
	replace edu_years = 12 if isced97 == 5 //level 3B
	replace edu_years = 13 if isced97 == 6 //level 3A
	replace edu_years = 16 if isced97 == 7 //level 4A
	replace edu_years = 16 if isced97 == 8 //level 5B
	replace edu_years = 17 if isced97 == 9 //level 5A
	replace edu_years = 20 if isced97 == 10 //level 6

*collapse isced97 for Fig 3
gen isced_int = 1 if inrange(isced97,0,4) // lower secondary or less
replace isced_int = 2 if inrange(isced97,5,6) // upper secondary
replace isced_int = 3 if isced97 == 7 // post-secondary
replace isced_int = 4 if inrange(isced97,8,10) // tertiary

*russia version
gen isced_rus = 1 if inrange(isced97,0,4) // lower secondary or less
replace isced_rus = 2 if inrange(isced97,5,6) // upper secondary
replace isced_rus = 3 if inrange(isced97,7,10) // tertiary incl. post-secondary (not separately categorized in russia census)

*collapse isced97 for Table B4
gen isced97_5a = isced97 >= 9 & isced97 != .
gen isced97_5b = isced97 >= 8 & isced97 != .
gen isced97_4 = isced97 >= 7 & isced97 != .
gen isced97_3a = isced97 >= 6 & isced97 != .
gen isced97_3b = isced97 >= 5 & isced97 != .

** Outcome: employment
gen employed = (ERWERBSTAT_KURZ == 11)

** Outcome: occupation (high medium low ISCO)
gen high_occ = inlist(BERUF_ISCO_HAUPTGR,1,2,3)
	replace high_occ = 0 if employed == 0
gen med_occ = inlist(BERUF_ISCO_HAUPTGR,4,5,6,7,8)
	replace med_occ = 0 if employed == 0
gen high_med_occ = max(high_occ,med_occ)

*bins for control vars
gen age_bin = floor(age/5)*5 //5-yr bins
gen years_in_germany_bin = floor(years_in_germany/5)*5 //5-yr bins
	replace years_in_germany_bin = years_in_germany if years_in_germany<=5 //1-yr bins for first 5 years


********************************************************************************************************************
*** II. Data analysis
********************************************************************************************************************

drop if WOHN_STAT_STP == 2 //population not at main residence - not part of census sampling frame

** Sample selection
gen sample = (age >= 25 & age <= 65)
	replace sample = 0 if native == 0 & Imm == 0 // include only natives, W. Europeans, and CB migrants
	replace sample = 0 if Imm == 1 & immigration_age <= 24 //exclude those arriving before age 25
	replace sample = 0 if Imm == 1 & (immigration_year > 2003) // exclude those who came after 2003, when many CB countries join EU
	replace sample = 0 if Imm == 1 & (immigration_year < 1962) // exclude those who came before 1962 (first post-berlin wall year)
	replace sample = 0 if Imm == 1 & (immigration_year == . | immigration_age == .) //can't use these in analysis

*randomly sample 10% of natives, stratified by sample==1
set seed 123456789
bys sample: drop if native == 1 & runiform()>0.1
replace weight = weight * 10 if native == 1 // change weight to reflect sampling of natives	
	
gen sample2 = (age >= 25 & age <= 65)
	replace sample2 = 0 if native == 0 & Imm2 == 0 // include only natives, W. Europeans, and CB migrants
	replace sample2 = 0 if Imm2 == 1 & immigration_age <= 24 //exclude those arriving before age 25
	replace sample2 = 0 if Imm2 == 1 & (immigration_year > 2003) // exclude those who came after 2003, when many CB countries join EU
	replace sample2 = 0 if Imm2 == 1 & (immigration_year < 1973) // exclude those who came before 1973 (first major expansion of EU)
	replace sample2 = 0 if Imm2 == 1 & (immigration_year == . | immigration_age == .) //can't use these in analysis
	
gen sample_allages = 1 //to check selection within younger group
	replace sample_allages = 0 if native == 0 & Imm == 0
	replace sample_allages = 0 if Imm == 1 & (immigration_year > 2003)
	replace sample_allages = 0 if Imm == 1 & (immigration_year < 1962)
	replace sample_allages = 0 if Imm == 1 & (immigration_year == . | immigration_age == .)
	
*global with universal controls
global controls_sel i.age_bin##female
global controls_out i.age_bin##female i.years_in_germany_bin##female

********************************************************************************************************************
*** a. Weighted analyses ***

** declare survey design
svyset psuid [pw = weight], fpc(f) singleunit(scaled)

*** Table 1 (Educational Attainment)
capture noisily svy: mean edu_years if sample == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]

* vs natives only, 2 then 3 period
capture noisily svy, subpop(sample if max(native,EB)==1): reg edu_years EB_post_93 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\t1.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93) keep(EB_post_93)

capture noisily svy, subpop(sample if max(native,EB)==1): reg edu_years EB_post_93 EB_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\t1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87) keep(EB_post_93 EB_post_87)

* DID vs W Europeans, 2 then 3 periods, w/ and w/o controls
capture noisily svy, subpop(sample): reg edu_years EB_post_93 Imm_post_93 i.birth_country
capture noisily outreg2 using "$outputpath\t1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 Imm_post_93) keep(EB_post_93 Imm_post_93)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 Imm_post_93 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\t1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 Imm_post_93) keep(EB_post_93 Imm_post_93)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country
capture noisily outreg2 using "$outputpath\t1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\t1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily erase "$outputpath\t1.txt"


*** Fig 3 (Selection from origin using Bayes' rule) + Fig B2 + Fig B3
preserve
cd "$outputpath"
keep if EB == 1

* Poland
putexcel set germany_poland_all, replace

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 152 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Hungary
putexcel set germany_hungary_all, replace

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 165 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Romania
putexcel set germany_romania_all, replace

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)


* Romanian Jews
putexcel set germany_romania_jews, replace

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & jewish == 1 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)


* Romanian ethnic Germans
putexcel set germany_romania_germans, replace

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_int if ZUZUG_STAAT == 154 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)



* Russia 2010 census
putexcel set germany_russia_all, replace

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_rus if ZUZUG_STAAT == 160 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)


* Russia (full USSR) 2010 Germans
putexcel set germany_russia_germans, replace

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & STAATSANGE_LAND == 0 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

* Russia (full USSR) 2010 Jews
putexcel set germany_russia_jews, replace

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1946 & GEBOREN_JAHR <= 1986 & immigration_age >= 25 & immigration_year <= 2010 //age 25-65 as of 2011
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel A1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 1986 //age 25-45 in 1970, hadn't migrated by 1970, migrated between 1970-1986
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel C1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1925 & GEBOREN_JAHR <= 1945 & immigration_year >= 1970 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel E1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 1992 //age 25-45 in 1987, hadn't migrated by 1987, migrated between 1987-1992
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel G1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1942 & GEBOREN_JAHR <= 1962 & immigration_year >= 1987 & immigration_year <= 2010 //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel I1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2003  //age 25-45 in 1993, hadn't migrated by 1993, migrated between 1993-2003
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel K1=matrix(A)

capture noisily svy: tab isced_rus if Sov == 1 & jewish == 1 & GEBOREN_JAHR >= 1948 & GEBOREN_JAHR <= 1968 & immigration_year >= 1993 & immigration_year <= 2010  //for denominator
matrix A  = (e(Row)', hadamard(e(Prop),J(e(r),1,e(N_pop))))
putexcel M1=matrix(A)

restore

*** Table 2 (Labor Market Outcomes)
local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ {
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}
	capture noisily svy: mean `outcome' if sample == 1 & EB == 1
	capture noisily local depvar_mean_eb = e(b)[1,1]

	capture noisily svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_out
	capture noisily outreg2 using "$outputpath\t2.xls", `append_replace' label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}
capture noisily erase "$outputpath\t2.txt"

*** Fig B1 -- Coefficients on Edu by Year

capture noisily svy, subpop(sample if immigration_year<=1998): reg edu_years EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\figb1.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86) keep(EB_98 EB_97 EB_96 EB_95 EB_94 EB_93 EB_92 EB_91 EB_90 EB_89 EB_88 EB_87 EB_86 Imm_98 Imm_97 Imm_96 Imm_95 Imm_94 Imm_93 Imm_92 Imm_91 Imm_90 Imm_89 Imm_88 Imm_87 Imm_86)

capture noisily erase "$outputpath\figb1.txt"

*** Fig B4 -- dropping one origin country at a time
local append_replace replace
foreach country in 127 139 142 146 160 166 169 422 425 430 444 450 470 477 741 471 125 152 154 155 164 165{
capture noisily svy, subpop(sample if ZUZUG_STAAT != `country'): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\figb4.xls", `append_replace' label stats(coef se) nonotes dec(3) afmt(g) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
local append_replace append
}

capture noisily erase "$outputpath\figb4.txt"

*** Table B1 -- sum stats
*pull subpop sizes in 10,000,000s
capture noisily svy, subpop(sample if native == 1): reg female 
	local obs_1 = e(N_sub)/10000000
	local pop_1 = e(N_subpop)/10000000
capture noisily svy, subpop(sample if west == 1): reg female 
	local obs_2 = e(N_sub)/10000000
	local pop_2 = e(N_subpop)/10000000
capture noisily svy, subpop(sample if Sat == 1): reg female 
	local obs_3 = e(N_sub)/10000000
	local pop_3 = e(N_subpop)/10000000
capture noisily svy, subpop(sample if Sov == 1): reg female 
	local obs_4 = e(N_sub)/10000000
	local pop_4 = e(N_subpop)/10000000


capture noisily svy, subpop(sample if native == 1): mean immigration_age female married edu_years isced97_5a employed high_occ
capture noisily outreg2 using "$outputpath\tb1.xls", replace label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_1', "Subpop Size (10000000s)", `pop_1') dec(2) adec(4)
capture noisily svy, subpop(sample if west == 1): mean immigration_age female married edu_years isced97_5a employed high_occ
capture noisily outreg2 using "$outputpath\tb1.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_2', "Subpop Size (10000000s)", `pop_2') dec(2) adec(4)
capture noisily svy, subpop(sample if Sat == 1): mean immigration_age female married edu_years isced97_5a employed high_occ
capture noisily outreg2 using "$outputpath\tb1.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_3', "Subpop Size (10000000s)", `pop_3') dec(2) adec(4)
capture noisily svy, subpop(sample if Sov == 1): mean immigration_age female married edu_years isced97_5a employed high_occ
capture noisily outreg2 using "$outputpath\tb1.xls", append label stats(coef) nonotes noaster addstat("Subpop Obs (10000000s)", `obs_4', "Subpop Size (10000000s)", `pop_4') dec(2) adec(4)

cap erase "$outputpath\tb1.txt"


*** Table B4 Different measures of education
local iter = 0
local append_replace "replace"
foreach outcome in isced97_3b isced97_3a isced97_4 isced97_5b isced97_5a{
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}
	capture noisily svy: mean `outcome' if sample == 1 & EB_post_87 == 1 & EB_post_93 == 0 //want late refugee mean
	capture noisily local depvar_mean_eb = e(b)[1,1]	
	capture noisily svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
	capture noisily outreg2 using "$outputpath\tb4.xls", `append_replace' label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}
capture noisily erase "$outputpath\tb4.txt"


*** Table B5 Sensitivity analysis
gen middle = Imm == 1 & (immigration_year>=1980 & immigration_year<=1999) // to focus on middle period
gen exclude_35 = Imm == 1 & immigration_age < 35 //to exclude those arriving before 35
gen exclude_jewish = jewish == 1 //to exclude jewish immigrants

**Construct linear pre-period trend
gen trend = (immigration_year - 1986) * post_87
replace trend = 0 if Imm == 0
gen EB_trend = EB * (immigration_year - 1960) if Imm == 1
gen WE_trend = (1 - EB) * (immigration_year - 1960) if Imm == 1
gen prtt_edu_years = 0
capture noisily reg edu_years EB_trend WE_trend i.birth_country $controls_sel [pweight = weight] if inrange(immigration_year,1961,1986) & sample == 1
capture noisily	replace prtt_edu_years = _b[EB_trend] * trend if post_87 == 1 & EB == 1
capture noisily	replace prtt_edu_years = _b[WE_trend] * trend if post_87 == 1 & EB == 0
drop EB_trend WE_trend trend

*Regs
capture noisily svy: mean edu_years if sample == 1 & EB == 1 & middle == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if middle==1 | Imm == 0): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb5.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily svy: mean edu_years if sample == 1 & EB == 1 & exclude_35 != 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if exclude_35!=1): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily svy: mean edu_years if sample == 1 & EB == 1 & exclude_jewish != 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if exclude_jewish!=1): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily svy: mean edu_years if sample2 == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample2): reg edu_years EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87) keep(EB_post_93 EB_post_87 Imm2_post_93 Imm2_post_87)

*top code at 80 since using full pop
gen age_tc = age 
replace age_tc = 80 if age_tc > 80 & age_tc != .
gen age_bin_tc = floor(age_tc/5)*5
capture noisily svy: mean edu_years if sample_allages == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample_allages): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country i.age_bin_tc female
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
drop age_tc age_bin_tc

capture noisily svy: mean edu_years if sample == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_89 Imm_post_93 Imm_post_89 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_89 Imm_post_93 Imm_post_89) keep(EB_post_93 EB_post_89 Imm_post_93 Imm_post_89)

capture noisily svy: mean edu_years if sample == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel prtt_edu_years
capture noisily outreg2 using "$outputpath\tb5.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 prtt_edu_years) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 prtt_edu_years)

drop middle exclude_35 exclude_jewish prtt_edu_years

capture noisily erase "$outputpath\tb5.txt"


*** Table B6 Outcomes w/ education as control
local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ {
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}
	capture noisily svy: mean `outcome' if sample == 1 & EB == 1
	capture noisily local depvar_mean_eb = e(b)[1,1]
	capture noisily svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_out i.edu_years
	capture noisily outreg2 using "$outputpath\tb6.xls", `append_replace' label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)
}
capture noisily erase "$outputpath\tb6.txt"


*** Table B7 -- Sat/Sov separately & excluding recovery
	capture noisily svy: mean edu_years if sample == 1 & Sat == 1
	capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if Sov == 0): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb7.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

	capture noisily svy: mean edu_years if sample == 1 & Sov == 1
	capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if Sat == 0): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb7.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

	capture noisily svy: mean edu_years if sample == 1 & Sat == 1 & immigration_year <= 1999
	capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if Sov == 0 & (native == 1 | immigration_year <= 1999)): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb7.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

	capture noisily svy: mean edu_years if sample == 1 & Sov == 1 & immigration_year <= 1999
	capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample if Sat == 0 & (native == 1 | immigration_year <= 1999)): reg edu_years EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\tb7.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily erase "$outputpath\tb7.txt"

*** Table B9 -- Gender
gen EB_post_93_m = EB_post_93 * female==0
gen EB_post_87_m = EB_post_87 * female==0
gen Imm_post_93_m = Imm_post_93 * female==0
gen Imm_post_87_m = Imm_post_87 * female==0
gen EB_m = EB * female==0
gen Imm_m = Imm * female==0

local iter = 0
local append_replace "replace"
foreach outcome in employed high_occ high_med_occ {
	
local iter = `iter' + 1

if `iter' == 2{
	local append_replace "append"
}

capture noisily svy: mean `outcome' if sample == 1 & EB == 1
capture noisily local depvar_mean_eb = e(b)[1,1]
capture noisily svy, subpop(sample): reg `outcome' EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m i.birth_country $controls_out
capture noisily outreg2 using "$outputpath\tb9.xls", `append_replace' label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)', "depvar_mean_eb", `depvar_mean_eb') sortvar(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87) keep(EB_post_93_m EB_post_87_m EB_post_93 EB_post_87)
}

capture noisily erase "$outputpath\tb9.txt"
drop EB_post_93_m EB_post_87_m Imm_post_93_m Imm_post_87_m EB_m Imm_m

*** Table D1 -- restructuring (heterogeneous effects)

* Selection+Outcomes by gender
gen EB_post_93_m = EB_post_93 * female==0
gen EB_post_87_m = EB_post_87 * female==0
gen Imm_post_93_m = Imm_post_93 * female==0
gen Imm_post_87_m = Imm_post_87 * female==0
gen EB_m = EB * female==0
gen Imm_m = Imm * female==0

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\td1.xls", replace label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m) keep(EB_post_93 EB_post_87 EB_post_93_m EB_post_87_m EB_m Imm_post_93 Imm_post_87 Imm_post_93_m Imm_post_87_m Imm_m)
drop EB_post_93_m EB_post_87_m Imm_post_93_m Imm_post_87_m EB_m Imm_m

* Selection+Outcomes by age
gen EB_post_93_y = EB_post_93 * (immigration_age<=34)
gen EB_post_87_y = EB_post_87 * (immigration_age<=34)
gen Imm_post_93_y = Imm_post_93 * (immigration_age<=34)
gen Imm_post_87_y = Imm_post_87 * (immigration_age<=34)
gen EB_y = EB * (immigration_age<=34)
gen young = (immigration_age<=34)

capture noisily svy, subpop(sample): reg edu_years EB_post_93 EB_post_87 EB_post_93_y EB_post_87_y EB_y Imm_post_93 Imm_post_87 Imm_post_93_y Imm_post_87_y i.birth_country young $controls_sel
capture noisily outreg2 using "$outputpath\td1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)') sortvar(EB_post_93 EB_post_87 EB_post_93_y EB_post_87_y EB_y Imm_post_93 Imm_post_87 Imm_post_93_y Imm_post_87_y) keep(EB_post_93 EB_post_87 EB_post_93_y EB_post_87_y EB_y Imm_post_93 Imm_post_87 Imm_post_93_y Imm_post_87_y)
drop EB_post_93_y EB_post_87_y Imm_post_93_y Imm_post_87_y EB_y young

* Selection+Outcomes by Sov/Sat
capture noisily svy, subpop(sample): reg edu_years Sov_post_93 Sov_post_87 EB_post_93 EB_post_87 Imm_post_93 Imm_post_87 i.birth_country $controls_sel
capture noisily outreg2 using "$outputpath\td1.xls", append label stats(coef se) nonotes dec(3) addstat("Subpop Obs", `e(N_sub)') sortvar(Sov_post_93 Sov_post_87 EB_post_93 EB_post_87 Imm_post_93 Imm_post_87) keep(Sov_post_93 Sov_post_87 EB_post_93 EB_post_87 Imm_post_93 Imm_post_87)

capture noisily erase "$outputpath\td1.txt"
